package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.mapper.AlbumAttributeValueMapper;
import com.atguigu.tingshu.album.service.AlbumAttributeValueService;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.login.MyLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"all"})
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @Operation(summary = "保存专辑")
    @PostMapping("saveAlbumInfo")
    @MyLogin
    public Result<Boolean> saveAlbumInfo(@RequestBody @Validated AlbumInfoVo albumInfoVo) {
        return Result.ok(albumInfoService.saveAlbumInfo(albumInfoVo));
    }

    @MyLogin
    @Operation(summary = "获取当前用户专辑分页列表")
    @PostMapping("findUserAlbumPage/{page}/{limit}")
    public Result findUserAlbumPage(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,
            @Parameter(name = "albumInfoQuery", description = "查询对象", required = false)
            @RequestBody AlbumInfoQuery albumInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        albumInfoQuery.setUserId(userId);
        IPage<AlbumListVo> iPage = albumInfoService.findUserAlbumPage(page, limit, albumInfoQuery);
        return Result.ok(iPage);
    }

    @Operation(summary = "删除专辑信息")
    @DeleteMapping("removeAlbumInfo/{id}")
    public Result<Boolean> removeAlbumInfo(@PathVariable(value = "id") Long id) {
        return Result.ok(albumInfoService.removeAlbumInfo(id));
    }

    @Operation(summary = "通过id获取专辑信息")
    @GetMapping("getAlbumInfo/{id}")
    public Result<AlbumInfo> getAlbumInfo(@PathVariable("id") Long id) {
        return Result.ok(albumInfoService.getAlbumInfo(id));
    }

    @Operation(summary = "修改专辑")
    @PutMapping("/updateAlbumInfo/{id}")
    public Result<Boolean> updateAlbumInfo(@PathVariable("id") Long id,
                                           @RequestBody @Validated AlbumInfoVo albumInfoVo) {
        return Result.ok(albumInfoService.updateAlbumInfo(id, albumInfoVo));
    }

    @Operation(summary = "获取当前用户的专辑列表")
    @GetMapping("findUserAllAlbumList")
    @MyLogin
    public Result<List<AlbumInfo>> findUserAllAlbumList() {
        return Result.ok(albumInfoService.findUserAllAlbumList());
    }

    @Autowired
    AlbumAttributeValueService albumAttributeValueService;


    //返回的是List因为专辑与专辑属性是一对多关系，所以一个albumid对应多条AlbumAttributeValue
    //它有男频 音乐 等属性
    @Operation(summary = "根据专辑Id获取专辑属性信息")
    @GetMapping("findAlbumAttributeValue/{albumId}")
    public Result<List<AlbumAttributeValue>> findAlbumAttributeValue(@PathVariable("albumId") Long albumId) {
        return Result.ok(albumAttributeValueService.findAlbumAttributeValueByAlbumId(albumId));
    }

    //根据专辑id获取四项数据（专辑统计，评论数啥的）
    @Operation(summary = "根据专辑Id获取专辑统计数据")
    @GetMapping("getAlbumStat/{albumId}")
    public Result<Map<String, Object>> getAlbumStat(@PathVariable("albumId") Long albumId) {
        return Result.ok(albumInfoService.getAlbumStat(albumId));
    }

    @Operation(summary = "获取到专辑统计信息")
    @GetMapping("/getAlbumStatVo/{albumId}")
    public Result getAlbumStatVo(@PathVariable Long albumId){
        //获取服务层方法
        AlbumStatVo albumStatVo = this.albumInfoService.getAlbumStatVoByAlbumId(albumId);
        return Result.ok(albumStatVo);
    }

}

